<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h3>猜数字游戏</h3>

    <div>
        <button id="reset">重新开始一局游戏</button>
    </div>
    <div>
        请输入要猜的数字：
        <input type="text" id="guessNum">
        <input type="button" id="guessBtn" value="猜">
    </div>
    <div>
        已经猜了的次数：<span id="times"></span>
    </div>
    <div>
        结果：<span id="result"></span>
    </div>
</body>
<script>
    //1.获取相关的标签
    let resetEl = document.querySelector('#reset');
    let guessNumEl = document.querySelector('#guessNum');
    let guessBtnEl = document.querySelector('#guessBtn');
    let timesEl = document.querySelector('#times');
    let resultEl = document.querySelector('#result');
    //2.生成一个随机数
    //生成一个0-1的小数
    let num = Math.random();
    //化为一个整数
    num = parseInt(num * 100) + 1;
    console.log(num);
    let count = 0;

    guessBtnEl.onclick = function(){
        count++;
        timesEl.innerHTML = count + '次';
        let guessNum = guessNumEl.value;
        if(guessNum > num){
            resultEl.innerHTML = '猜大了';
        }else if(guessNum < num){
            resultEl.innerHTML = '猜小了';
        }else{
            resultEl.innerHTML = '猜对了！！！';
        }

        resetEl.onclick = function(){
            count = 0;
            guessNumEl.value = 0;
            timesEl.innerHTML = '';
            resultEl.innerHTML = '';
        }
    }
</script>
</html>